<template>
	<view class="study-video-container">
		<view class="media-box">
			<video id="myVideo" class="video" :src="encodeURI(fileUrl)" @error="videoErrorCallback" @play="startTime"
				@pause="saveTime" controls ></video>
		</view>

		<view class="bg-white mt-10">
			<view class="py-40 px-30 w-full box-content flex bg-studying">
				<view class="info ml-20">
					<view class="title text-bold">{{ detail.coursewareName || '--' }}</view>
					<view class="time text-gray">教师：{{ detail.teacherName || '--' }}</view>
					<view class="time text-gray">总时长：{{ detail.learningDurationLabel || '--' }}</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import config from '@/config'
	import {
		getCoursewareDetail,
		coursewareLearn
	} from '@/api/studyGarden/index.js'

	export default {
		data() {
			return {
				id: '',
				title: '',
				fileUrl: '',
				detail: {
					
				},
				timeCount: 0,
				timeCountTimer: null,

				autoSaveTimer: null,
				autoSaveTime: 30 // 单位秒
			}
		},
		onLoad(params) {
			if (params.id) {
				this.id = params.id
				this.getDetail()
			}
		},
		onHide() {
			console.log('onHide');
			this.videoPause()
		},
		beforeDestroy() {
			this.saveTime()
			clearInterval(this.autoSaveTimer)
		},
		methods: {
			videoPause() {
				const videoContext = uni.createVideoContext('myVideo');
				videoContext.pause();
			},
			videoErrorCallback(err) {
				console.log(err);
			},
			getDetail() {
				getCoursewareDetail(this.id).then(res => {
					if (res.data.fileUrl) {
						this.detail = res.data
						const url = res.data.fileUrl
						this.fileUrl = `${config.staticUrl}${url}`
					} else {
						uni.showToast({
							title: '文件地址错误',
							icon: 'none'
						})
					}
				})
			},
			startTime() {
				if (this.timeCountTimer) {
					return
				}
				this.timeCount = 0;
				clearInterval(this.timeCountTimer)
				this.timeCountTimer = setInterval(() => {
					++this.timeCount
				}, 1000);

				// 定时上传进度
				this.autoSaveTimer = setInterval(() => {
					this.saveTime()
					this.startTime()
				}, this.autoSaveTime * 1000);
			},
			saveTime() {
				clearInterval(this.timeCountTimer)
				this.timeCountTimer = null
				clearInterval(this.autoSaveTimer)
				this.autoSaveTimer = null
				coursewareLearn({
					coursewareId: this.id,
					learnedDuration: this.timeCount,
					showLoading: false
				}).then(() => {
					console.log('时长保存成功');
					this.timeCount = 0
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.study-video-container {
		font-size: 0;
	}
	
	.media-box .video {
		width: 100%;
		height: 450rpx;
	}
	
	.text-bold {
		font-size: 36rpx;
	}
	
	.mt-14 {
		margin-top: 14rpx;
	}
	
	.text-gray {
		font-size: 24rpx;
		color: #999999;
	}
	
	.bg-studying {
		background: #ebf2f5;
	}
	
	.box-content {
		position: relative;
		height: 195rpx;

		.title {
			font-size: 30rpx;
			word-break: break-all;
		}
	
		.time {
			padding-left: 28rpx;
			margin-top: 4rpx;
		}
	
		.status {
			position: absolute;
			bottom: 15rpx;
			right: 30rpx;
		}
	
		.progress {
			left: 0;
			position: absolute;
			width: 100%;
			bottom: 0;
		}
	}
</style>